package indi.coffeenc.queuedownloader.strategy;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class SimpleTaskHolderStrategy implements TaskHolderStrategy {
    /**
     * 待下载队列
     */
    private final LinkedHashMap<String,Task> WAITING = new LinkedHashMap<String, Task>();

    /**
     * 正在下载队列
     */
    private final LinkedHashMap<String,Task> DOWNLOADING = new LinkedHashMap<String, Task>();

    /**
     * 暂停队列
     */
    private final LinkedHashMap<String,Task> PAUSED = new LinkedHashMap<String, Task>();

    public void add(Task task) {
        WAITING.put(task.getUuid(),task);
    }

    public void delete(String uuid) {
        /**
         * 只删除等待队列和暂停队列中的任务，下载中的任务先执行暂停操作
         */
        if (WAITING.containsKey(uuid))
            WAITING.remove(uuid);
        else
            PAUSED.remove(uuid);
    }

    public void pause(String uuid) {
        Task task;
        if (WAITING.containsKey(uuid)){
            task = WAITING.get(uuid);
            WAITING.remove(uuid);
        }else {
            task = DOWNLOADING.get(uuid);
            DOWNLOADING.remove(uuid);
        }
        PAUSED.put(task.getUuid(),task);
    }

    public void start(String uuid) {
        Task task = PAUSED.get(uuid);
        if (task == null)
            return;
        WAITING.put(task.getUuid(),task);
        PAUSED.remove(uuid);
    }

    public int progress(String uuid) {
        /**
         * 只获取正在下载队列中的内容
         */
        Task task = DOWNLOADING.get(uuid);
        return (int)(task.getDone()/(task.getTotal()/100));
    }

    public String getAll() {
        return this.toString();
    }

    public Task get() {
        for(Map.Entry entry: WAITING.entrySet()){
            Task task = (Task) entry.getValue();
            WAITING.remove(task.getUuid());
            DOWNLOADING.put(task.getUuid(),task);
            return task;
        }
        return null;
    }

    public boolean isEmpty() {
        return WAITING.isEmpty();
    }

    public boolean exists(String source) {
        return exists(DOWNLOADING,source)||exists(WAITING,source)||exists(PAUSED,source);
    }

    private boolean exists(LinkedHashMap<String ,Task> map , String source){
        for (Map.Entry entry : map.entrySet())
            if (((Task)entry.getValue()).getSource().equals(source))
                return true;
        return false;
    }

    @Override
    public String toString() {
        return "{"
                + "\"waiting\":"
                + mapToJson(WAITING)
                + ",\"downloading\":"
                + mapToJson(DOWNLOADING)
                + ",\"paused\":"
                + mapToJson(PAUSED)
                + '}';
    }

    public String mapToJson(HashMap<String,Task> map){
        String string = "{";
        boolean b = false;
        for (Map.Entry entry:map.entrySet()){
            if (b){
                string+=",";
            }
            string+="\""+entry.getKey()+"\":"+entry.getValue();
            b = true;
        }
        string+="}";
        return string;
    }

}
